<?php

/**
 * 会员登录
 */

namespace app\member\common;

class LoginCommon extends \app\member\common\MemberCommon {


    protected $noLogin = true;
    protected $nameTip = '手机号/邮箱';

    public function __construct() {
        parent::__construct();
        $config = target('member/memberConfig')->getConfig();
        if($config['reg_type'] == 'all') {
            $this->nameTip = '手机号/邮箱';
        }
        if($config['reg_type'] == 'email') {
            $this->nameTip = '邮箱';
        }
        if($config['reg_type'] == 'tel') {
            $this->nameTip = '手机';
        }
    }

    protected function index() {
        if (!isPost()) {
            $this->setMeta('登录');
            $this->pageInfo['name'] = '登录';
            $loginList = target('member/MemberConnect')->getLoginList($this->platform);
            if (!empty($loginList)) {
                foreach ($loginList as $vo) {
                    if ($vo['jump']) {
                        $this->redirect($vo['url']);
                    }
                }
            }
            $this->assign('nameTip', $this->nameTip);
            $this->assign('loginList', $loginList);
            $this->otherDisplay();
        } else {
            $userName = request('post', 'username');
            $password = request('post', 'password');
            $remember = request('post', 'remember', 0);
            if (empty($userName) || empty($password)) {
                $this->error('用户名或密码未填写！');
            }
            $loginData = target('member/Member', 'service')->loginUser($userName, $password);
            if (!$loginData) {
                $this->error(target('member/Member', 'service')->getError());
            }
            $time = $remember ? 2592000 : 3600;
            \Dux::cookie()->set('user_login', [
                'uid' => $loginData['uid'],
                'token' => $loginData['token']
            ], $time);
            $this->success('账号登录成功!', $this->action ? $this->action : url('member/Index/index'));
        }
    }

    protected function bind() {
        $openId = request('', 'open_id', 0, 'html_clear');
        $type = request('', 'type', 0, 'html_clear');
        $bind = request('', 'bind', 0, 'intval');
        if (empty($openId) || empty($type)) {
            $this->error('oath参数错误!');
        }
        $loginInfo = target('member/MemberConnect')->getWhereInfo([
            'open_id' => $openId,
            'type' => $type
        ]);
        if (empty($loginInfo)) {
            $this->error('第三方登录信息不存在!');
        }
        if ($loginInfo['user_id']) {
            $this->error('已绑定会员账号', url('member/Login/index'));
        }

        $oathData = unserialize($loginInfo['data']);
        if (!isPost()) {
            $this->setMeta('绑定账号');
            $this->pageInfo['name'] = '绑定账号';
            $urlParams = [
                'open_id' => $openId,
                'type' => $type,
                'bind' => $bind
            ];
            $hookHtml = '';
            if(!$bind) {
                if(LAYER_NAME == 'mobile') {
                    $hookList = run('service', 'member', 'regMobileHtml');
                }else {
                    $hookList = run('service', 'member', 'regHtml');
                }
                foreach ($hookList as $app => $vo) {
                    if(!empty($vo)) {
                        $hookHtml .= $vo;
                    }
                }
            }
            $this->assign('nameTip', $this->nameTip);
            $config = target('member/MemberConfig')->getConfig();
            $this->assign('userConfig', $config);
            $this->assign('loginInfo', $loginInfo);
            $this->assign('urlParams', $urlParams);
            $this->assign('oathData', $oathData);
            $this->assign('bind', $bind);
            $this->assign('hookHtml', $hookHtml);
            $this->dialogDisplay();
        } else {
            $model = target('member/MemberUser');
            $model->beginTransaction();
            $userName = request('post', 'username');
            $password = request('post', 'password');
            $password2 = request('post', 'password2');
            $code = request('post', 'code');
            if (empty($userName) || empty($password)) {
                $this->error('用户名或密码未填写！');
            }

            if ($bind) {
                $loginData = target('member/Member', 'service')->loginUser($userName, $password);
                if (!$loginData) {
                    $model->rollBack();
                    $this->error(target('member/Member', 'service')->getError());
                }
                $userInfo = $loginData['info'];
                if (!empty($userInfo['avatar'])) {
                    $oathData['avatar'] = '';
                }
                if (!empty($userInfo['nickname'])) {
                    $oathData['nickname'] = '';
                }
            } else {
                if ($password <> $password2) {
                    $this->error('重复密码不正确!');
                }
                if (!request('post', 'agreement')) {
                    $this->error('请先阅读注册协议！');
                }
                $loginData = target('member/Member', 'service')->regUser($userName, $password, $code, $oathData['nickname']);
                if (!$loginData) {
                    $this->error(target('member/Member', 'service')->getError());
                }
            }

            if (!target('member/Member', 'service')->connectUser($loginInfo['connect_id'], $loginData['uid'], $oathData['nickname'], $oathData['avatar'])) {
                $model->rollBack();
                $this->error(target('member/Member', 'service')->getError());
            }
            $loginCookie = [
                'uid' => $loginData['uid'],
                'token' => $loginData['token']
            ];
            $model->commit();
            $time = 2592000;
            \Dux::cookie()->set('user_login', $loginCookie, $time);
            $this->success('登录成功！', url('member/Index/index'));
        }
    }

    protected function logout() {
        \Dux::cookie()->del('user_login');
        $this->redirect(url('index'));
    }

}